package com.sgb.kjwl.viewmodel.machine;

import android.text.TextUtils;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.lease.LeaseOrderEntity;
import com.sgb.kjwl.model.entity.lease.LeaseOrderInvoiceEntity;
import com.sgb.kjwl.model.entity.lease.OrderSnapshotEntity;
import com.sgb.kjwl.model.entity.lease.ProductPriceEntity;
import com.sgb.kjwl.model.entity.transport.MapLongAndLatEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.repository.MachineRepertory;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.self.lease.LeaseOrderDeviceActivity;
import com.sgb.kjwl.view.ui.activity.self.lease.LeaseOrderMachineActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import org.json.JSONObject;

import java.util.List;

import androidx.databinding.ObservableField;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LeaseOrderDeviceModel extends BaseViewModel {
    public ObservableField<LeaseOrderEntity> addEntity = new ObservableField<>(new LeaseOrderEntity());
    private LeaseOrderDeviceActivity activity;
    private MachineRepertory repertory;

    public LeaseOrderDeviceModel(LeaseOrderDeviceActivity activity, MachineRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    //租赁订单-新增
    public void leaseOrder() {
        DialogHelper.showProgressDialog(activity, "提交中...").setCanceledOnTouchOutside(false);
        addEntity.get().setModule("2");
        APIManager.getInstance().getLeaseAPI().leaseOrderAddOrUpdate(addEntity.get()).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response == null || response.body() == null) return;
                if (response.body() != null && response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    try {
                        JSONObject jsonObject = new JSONObject(response.body().getData().toString());
                        String orderNo = jsonObject.getString("orderNo");
                        activity.showSuccess(orderNo);
                        activity.finish();
                    } catch (Exception e) {
                    }
                } else {
                    if (response.body() != null && !TextUtils.isEmpty(response.body().getMsg())) {
                        MToast.showToast(activity, response.body().getMsg());
                    } else {
                        MToast.showToast(activity, "下单失败");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                MToast.showToast(activity, "发送服务器失败");
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //获取省市区
    public void getAddressModel() {
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                if (response == null || response.body() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.initAddressData(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }


    //获取经纬度信息
    public void getLocationLL(String provinceNo, String cityNo, String districtNo) {
        DialogHelper.showProgressDialog(activity, "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getDriverAPI().getLocationLL(repertory.getAreaCode(), provinceNo, cityNo, districtNo).enqueue(new Callback<BaseEntity<MapLongAndLatEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<MapLongAndLatEntity>> call, Response<BaseEntity<MapLongAndLatEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response == null || response.body() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.getLocationLL(response.body().getData());
                } else {
                    MToast.showToast(activity.getActivity(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<MapLongAndLatEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity.getActivity(), t.getMessage());
            }
        });

    }

    //获取企业发票列表
    public void getInvoiceList() {
        DialogHelper.showProgressDialog(activity, "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLeaseAPI().getInvoicelist(1, 100).enqueue(new Callback<BaseEntity<List<LeaseOrderInvoiceEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<LeaseOrderInvoiceEntity>>> call, Response<BaseEntity<List<LeaseOrderInvoiceEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response == null || response.body() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.setLeaseOrderInvoiceList(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<LeaseOrderInvoiceEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //获取个人发票列表
    public void getPersonInvoicelist() {
        DialogHelper.showProgressDialog(activity, "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLeaseAPI().getPersonInvoicelist(1, 100).enqueue(new Callback<BaseEntity<ListResponse<List<LeaseOrderInvoiceEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<LeaseOrderInvoiceEntity>>>> call, Response<BaseEntity<ListResponse<List<LeaseOrderInvoiceEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response == null || response.body() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.setLeaseOrderInvoiceList(response.body().getData().getList());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<LeaseOrderInvoiceEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //获取出租下单快照详情
    public void getSnapshot(String id, String model) {
        DialogHelper.showProgressDialog(activity, "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLeaseAPI().getSnapshot(id, model).enqueue(new Callback<BaseEntity<OrderSnapshotEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<OrderSnapshotEntity>> call, Response<BaseEntity<OrderSnapshotEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response == null || response.body() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showOrderMachineDeatilEntity(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<OrderSnapshotEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //获取出租信息价格单位组信息
    public void getPriceImg(String id, String model, String unitNo) {
        DialogHelper.showProgressDialog(activity, "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getLeaseAPI().getPriceImg(id, model, unitNo).enqueue(new Callback<BaseEntity<ProductPriceEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<ProductPriceEntity>> call, Response<BaseEntity<ProductPriceEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response == null || response.body() == null) return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showProductPriceEntity(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ProductPriceEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


    /**
     * 出租快照详情
     *
     * @param type
     * @param id
     */
    public void getH5UrlByOrder(String type, String id) {
        String name = "";
        if ("1".equals(type)) {
            name = "mechanicsDetail";//机械
        } else if ("2".equals(type)) {
            name = "equipmentDetails";//设备
        } else if ("3".equals(type)) {
            name = "vehicleDetails";//车辆
        } else if ("4".equals(type)) {
            name = "propertyDetails";//房产
        } else if ("5".equals(type)) {
            name = "landDetails";//土地
        } else if ("6".equals(type)) {
            name = "factoryDetails";//工厂
        } else if ("7".equals(type)) {
            name = "farmDetail";//农机
        }
        if (TextUtils.isEmpty(name)) {
            return;
        }
        DialogHelper.showProgressDialog(activity,  "加载中...").setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting(name).enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?no=" + id;
                    WebViewX5HostActivity.start(activity,url);
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

}
